DEPENDENCIES=(build-essential xorg-dev libudev-dev libts-dev libgl1-mesa-dev libglu1-mesa-dev libasound2-dev libpulse-dev libopenal-dev libogg-dev libvorbis-dev libaudiofile-dev libpng12-dev libfreetype6-dev libusb-dev libdbus-1-dev zlib1g-dev libdirectfb-dev libsdl2-dev libsdl2-2.0-0 libsdl2-image-dev libsdl2-image-2.0-0 libsdl2-mixer-2.0-0 libsdl2-mixer-dev libsdl2-ttf-2.0-0 libsdl2-ttf-dev libgtk-3-dev libgtkmm-3.0-dev)
# What dependencies are missing?
PKGSTOINSTALL=""
for (( i=0; i<${#DEPENDENCIES[*]}; i++ )); do
    if [[ `dpkg -l | grep -w "ii  ${DEPENDENCIES[i]} "` ]]; then
        PKGSTOINSTALL=$PKGSTOINSTALL" "${DEPENDENCIES[i]}
    # If it's impossible to determine if there are missing dependencies, mark all as missing
    else
        PKGSTOINSTALL=$PKGSTOINSTALL" "${DEPENDENCIES[i]}
    fi
done
# If some dependencies are missing, installs them
if [ "$PKGSTOINSTALL" != "" ]; then
    echo "Some dependencies are missing. Installing... "
    # Debian, Ubuntu and derivatives (with apt-get)
    if [[ `which apt-get` ]]; then
        sudo apt-get install $PKGSTOINSTALL
    else
        echo "ERROR: impossible to found a package manager in your sistem. Please, install manually ${DEPENDENCIES[*]}."
        exit 1
    fi
    echo "Done installing."
fi

 If everything is OK, compile and install

cd client
make
cd ../server
make
cd ../editor
make
cd ..
if [ ! -d release ]; then
	mkdir release
fi
if [ ! -d release/client ]; then
	mkdir release/client
	mkdir -p release/client/res
	mkdir -p release/client/sound
	mkdir -p release/client/previews
fi
if [ ! -d release/server ]; then
	mkdir release/server
fi
if [ ! -d release/editor ]; then
	mkdir release/editor
fi

cp client/client client/application.properties release/client
cp client/res/* release/client/res
cp client/sound/* release/client/sound
cp client/previews/* release/client/previews
cp editor/editor editor/application.properties editor/*.xml release/editor
cp server/server server/application.properties server/*.xml release/server
